﻿
namespace tjb.Utility.Config
{
    using System;
    using System.Text;
    using System.Xml.Serialization;
    using System.Xml.Schema;
    using System.Xml;
    /// <summary>
    /// 用于序列化CData节点。不能包含<code >]] ></code>字符。
    /// by tangjingbo @2008/11/24
    /// </summary>
    /// 
    [Serializable]
    public class CDATA : IXmlSerializable, IEquatable<CDATA>, IComparable<CDATA>
    {
        private string _value;
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public CDATA() { }
        /// <summary>
        /// 带参数构造函数
        /// </summary>
        /// <param name="value"></param>
        public CDATA(string value)
        {
            this._value = value;
        }
        /// <summary>
        /// 
        /// </summary>
        public string Value
        {
            get { return _value; }
        }
        /// <summary>
        /// 长度
        /// </summary>
        public int Length
        {
            get
            {
                return this._value.Length;
            }
        }
        #region IXmlSerializable interface
        XmlSchema IXmlSerializable.GetSchema()
        {
            return null;
        }

        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            /***** 如果此节点中包含有多个节点须使用此方法。**/
            this._value = reader.ReadElementContentAsString();
            /* **********/
            //this.text = reader.ReadString();
            // reader.Read();
        }

        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            writer.WriteCData(this._value);
        }
        #endregion

        #region ToString()

        /// <summary>
        /// 重写 获取CData节点的 内容
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this != null ? this._value : string.Empty;
        }
        #endregion

        #region implicit operator

        /// <summary>
        /// 将 CDATA 对象隐式转换成 内容 字符串。
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static implicit operator string(CDATA element)
        {
            return (element == null) ? null : element._value;
        }
        /// <summary>
        /// 将 内容 对象隐式转换成 CDATA 字符串。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static implicit operator CDATA(string value)
        {
            return new CDATA(value);
        }
        /// <summary>
        /// 将 内容 对象隐式转换成 CDATA 字符串。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static implicit operator CDATA(Int32 value)
        {
            return new CDATA(value.ToString());
        }
        /// <summary>
        /// 将 内容 对象隐式转换成 CDATA 字符串。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static implicit operator CDATA(Int64 value)
        {
            return new CDATA(value.ToString());
        }
        /// <summary>
        /// 将 内容 对象隐式转换成 CDATA 字符串。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static implicit operator CDATA(double value)
        {
            return new CDATA(value.ToString());
        }
        #endregion

        #region IEquatable<CDATA> 成员
        /// <summary>
        /// 
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(CDATA other)
        {

            return (other != null) ? this._value.Equals(other._value) : false;
        }

        #endregion

        #region IComparable<CDATA> 成员
        /// <summary>
        /// 
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(CDATA other)
        {
            return (other != null) ? this._value.CompareTo(other._value) : 1;
        }

        #endregion


    }
}
